﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin1_Guide : System.Web.UI.Page
{
    eStoreDataContext db = new eStoreDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        rpt_Category.DataSource = db.Guides;
        rpt_Category.DataBind();
    }
    protected void btnxoa_Click(object sender, EventArgs e)
    {
        int guideid = int.Parse(((LinkButton)sender).CommandArgument.ToString());
        //1. tim
        var gui = db.Guides.Single(c => c.GuideId == guideid);
        //2. Xoa
        db.Guides.DeleteOnSubmit(gui);
        //3. luu
        db.SubmitChanges();
    }
    protected void lbtChinhSua_Click(object sender, EventArgs e)
    {
        panel_Edit.Visible = true;
        panel_Add.Visible = false;

        int guideid = int.Parse(((LinkButton)sender).CommandArgument.ToString());
        // tim 
        var gui = db.Guides.Single(c => c.GuideId == guideid);
        // load thong tin
        txt_Guidid_Edit.Text = guideid.ToString();
        txt_Name_Edit.Text = gui.Name;
        txt_Image_Edit.Text = gui.Image;
        txt_Birthday_Edit.Text = gui.Birthday.ToString();
        txt_Address_Edit.Text = gui.Address;
        txt_Phone_Edit.Text = gui.Phone;
    }
    protected void lbt_Add_New_Click(object sender, EventArgs e)
    {
        panel_Add.Visible = true;
        panel_Edit.Visible = false;
    }
    protected void btn_Save_Edit_Click(object sender, EventArgs e)
    {
        try
        {
            // tim 
            var gui = db.Guides.Single(c => c.GuideId == int.Parse(txt_Guidid_Edit.Text));
            gui.Name = txt_Name_Edit.Text;
            gui.Image = txt_Image_Edit.Text;
            gui.Phone = txt_Phone_Edit.Text;
            gui.Address = txt_Address_Edit.Text;
            gui.Sex = ddl_Sex_Edit.Text;
            //3 Luu
            db.SubmitChanges();
            panel_Edit.Visible = false;
            Response.Redirect("Guide.aspx");
        }
        catch (Exception ex)
        {
            if (ex.Message.Contains("PK_HuongDanVien"))
            {
                Response.Write("<script> alert('Chỉnh sửa không thành công');</script>");
                txt_Name_Edit.Focus();
            }
        }
    }
    protected void lbt_Clase_Edit_Click(object sender, EventArgs e)
    {
        panel_Edit.Visible = false;
    }
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        try
        {
            var guide = new Guide
            {
                Name = txt_Name.Text,
                Sex = ddl_Sex.Text,
                Birthday = DateTime.Parse(txt_Birthday.Text),
                Address = txt_Address.Text,
                Image = txt_ImageAdd.Text,
                Phone = txt_Phone.Text
            };
            //2.them vao
            db.Guides.InsertOnSubmit(guide);
            //3. luu
            db.SubmitChanges();
            txt_Name.Text = txt_ImageAdd.Text = txt_Phone.Text = txt_Address.Text = "";
            panel_Add.Visible = false;
            Response.Redirect("Guide.aspx");
        }
        catch (Exception ex)
        {
            if (ex.Message.Contains("Unique_Name_Hotel"))
            {
                Response.Write("<script> alert('Thêm mới không thành công! Tên đã tồn tại!');</script>");
            }
        }
    }
    protected void lbt_ClosAdd_Click(object sender, EventArgs e)
    {
        panel_Add.Visible = false;
    }
}